using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class PromptPanel : UIBase
{
    private Text txt;
    private CanvasGroup cg;

    [SerializeField]
    [Range(0, 3f)]
    private float showTime = 1f;
    private float timer = 0f;

    private void Awake()
    {
        Bind(UIEvent.PROMPT_MSG);
    }
    public override void Execute(int eventCode, object message)
    {
        switch (eventCode)
        {
            case UIEvent.PROMPT_MSG:
                PromptMsg promptMsg = message as PromptMsg;
                promptMessage(promptMsg.txt, promptMsg.color);
                break;
            default:
                break;
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        txt = transform.Find("Text").GetComponent<Text>();
        cg = transform.Find("Text").GetComponent<CanvasGroup>();
        cg.alpha = 0;
    }

    private void promptMessage(string text, Color color)
    {
        txt.text = text;
        txt.color = color;
        timer = 0f;
        cg.alpha = 0;
        StartCoroutine(promptAnim());
    }

    private IEnumerator promptAnim()
    {
        while (cg.alpha < 1f)
        {
            cg.alpha += Time.deltaTime * 2;
            yield return new WaitForEndOfFrame();
        }

        while (timer < showTime)
        {
            timer += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }
        while(cg.alpha > 0f)
        {
            cg.alpha -= Time.deltaTime * 2;
            yield return new WaitForEndOfFrame();
        }
    }


}
